package com.lastcrdg.textcustomview.CustomView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * author：liluo
 * e-mail：lastcrazysun@163.com
 * company：cusc
 * date：2017/10/10 13:55
 * description：
 */

public class TextPathOver extends View {
    private int width;
    private int height;
    private Paint mPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint mPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint mPaint3 = new Paint(Paint.ANTI_ALIAS_FLAG);

    public TextPathOver(Context context) {
        super(context);
    }

    public TextPathOver(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public TextPathOver(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initPaint() {
        mPaint1.setColor(Color.BLACK);
        mPaint1.setStyle(Paint.Style.FILL);
        mPaint2.setColor(Color.BLACK);
        mPaint2.setStyle(Paint.Style.STROKE);
        mPaint2.setStrokeWidth(4);
        mPaint3.setColor(Color.WHITE);
        mPaint3.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(width / 2, height / 2);
        canvas.drawColor(Color.WHITE);
        Path path1 = new Path();
        Path path2 = new Path();
        Path path3 = new Path();
        Path path4 = new Path();

        path1.addCircle(0, 0, 200, Path.Direction.CW);
        path2.addRect(0, -200, 200, 200, Path.Direction.CW);
        path3.addCircle(0, -100, 100, Path.Direction.CW);
        path4.addCircle(0, 100, 100, Path.Direction.CCW);

        path1.op(path2, Path.Op.DIFFERENCE);
        path1.op(path3, Path.Op.UNION);
        path1.op(path4, Path.Op.DIFFERENCE);

        RectF rectF = new RectF();
        path1.computeBounds(rectF, true);

        canvas.drawPath(path1, mPaint1);
        canvas.drawCircle(0, 0, 200, mPaint2);
        canvas.drawCircle(0, 100, 25, mPaint1);
        canvas.drawCircle(0, -100, 25, mPaint3);
        canvas.drawRect(rectF, mPaint2);
//        canvas.drawPath(path1, mPaint1);
//        canvas.drawPath(path2, mPaint2);
//        canvas.drawPath(path3, mPaint3);
//        canvas.drawPath(path4, mPaint4);
    }
}
